//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.link.core.util;

import com.link.core.util.redisclient.JedisClusterSingleton;
import com.link.core.util.redisclient.LinkRedisClient;
import com.link.core.util.redisclient.LinkRedisClusterClient;
import com.link.core.util.redisclient.LinkRedisSingleClient;
import java.util.HashSet;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

public final class RedisUtil {
    private static final Logger logger = LogManager.getLogger(RedisUtil.class);
    private static String ADDR = CoreYmlConstants.getApplicationRedisHost();
    private static String PORT = CoreYmlConstants.getApplicationRedisPort();
    private static String AUTH = CoreYmlConstants.getApplicationRedisPassword();
    private static int MAX_ACTIVE = -1;
    private static int MAX_IDLE = 20;
    private static int MAX_WAIT = 1000000;
    private static boolean TEST_ON_BORROW = true;
    private static int timeBetweenEvictionRunsMillis = 30000;
    private static boolean testWhileIdle = true;
    private static int numTestsPerEvictionRun = 10;
    private static int minEvictableIdleTimeMillis = 600000;
    private static JedisSentinelPool jedisSentinelPool;
    private static int TIMEOUT = 1000000;
    private static String currentHostMaster = null;

    public RedisUtil() {
    }

    public static LinkRedisClient getJedis() {
        Object redisClient;
        if (CoreYmlConstants.isApplicationRedisClusterState()) {
            redisClient = new LinkRedisClusterClient(getClusterJedis());
        } else {
            redisClient = new LinkRedisSingleClient(getSingleJedis());
        }

        return (LinkRedisClient)redisClient;
    }

    public static Jedis getJedis(int dbIndex) {
        Jedis jedis;
        if (CoreYmlConstants.isApplicationRedisClusterState()) {
            jedis = getSentinelJedis();
        } else {
            jedis = getSingleJedis();
        }

        jedis.select(dbIndex);
        return jedis;
    }

    private static Jedis getSingleJedis() {
        int port1 = Integer.valueOf(CoreYmlConstants.getApplicationRedisPort());
        Jedis jedis = new Jedis(CoreYmlConstants.getApplicationRedisHost(), port1, TIMEOUT);
        String redisPassword = CoreYmlConstants.getApplicationRedisPassword();
        if (redisPassword != null && !redisPassword.isEmpty()) {
            jedis.auth(redisPassword);
        }
        return jedis;
    }

    private static JedisCluster getClusterJedis() {
        JedisClusterSingleton jedisClusterSingleton = JedisClusterSingleton.getInstance();
        return jedisClusterSingleton.getJedisCluster();
    }

    private static Jedis getSentinelJedis() {
        if (jedisSentinelPool == null) {
            if (CoreYmlConstants.getApplicationRedisClusterUrls() == null || CoreYmlConstants.getApplicationRedisClusterUrls().isEmpty()) {
                throw new NullPointerException("applicationRedisClusterUrls为空，集群模式下的redis的url为空，请检查yml配置文件");
            }

            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(MAX_ACTIVE);
            jedisPoolConfig.setMaxIdle(MAX_IDLE);
            jedisPoolConfig.setMaxWaitMillis((long)MAX_WAIT);
            jedisPoolConfig.setTimeBetweenEvictionRunsMillis((long)timeBetweenEvictionRunsMillis);
            jedisPoolConfig.setMinEvictableIdleTimeMillis((long)minEvictableIdleTimeMillis);
            jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
            jedisPoolConfig.setTestWhileIdle(testWhileIdle);
            jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW);
            Set sentinels = new HashSet(CoreYmlConstants.getApplicationRedisClusterUrls());
            jedisSentinelPool = new JedisSentinelPool(CoreYmlConstants.getApplicationRedisClusterMasterName(), sentinels,
                    jedisPoolConfig, TIMEOUT, CoreYmlConstants.getApplicationRedisClusterPassword());
        }

        String currentHostMasterTemp = jedisSentinelPool.getCurrentHostMaster().toString();
        if (!StringUtils.equals(currentHostMasterTemp, currentHostMaster)) {
            logger.debug("主从已转换，" + currentHostMaster + " -----> " + currentHostMasterTemp);
        }

        currentHostMaster = currentHostMasterTemp;
        logger.debug("current master sentinel redis: " + currentHostMaster);
        Jedis jedis = jedisSentinelPool.getResource();
        return jedis;
    }

    public static void returnResource(LinkRedisClient jedis) {
        if (!CoreYmlConstants.isApplicationRedisClusterState()) {
            if (jedis == null) {
                return;
            }

            try {
                jedis.close();
            } catch (Exception var2) {
                Exception e = var2;
                logger.error("jedis回收失败，" + e.getMessage());
            }
        }

    }

    public static String getProperty(String propertyKey, String corpId) {
        LinkRedisClient jedis = getJedis();
        String key = "APPLICATION_PARM_" + corpId;
        String corpProperty = jedis.hget(key, propertyKey);
        if (StringUtils.isBlank(corpProperty)) {
            return jedis.hget("APPLICATION_PARM_ALL", propertyKey);
        } else {
            returnResource(jedis);
            return corpProperty;
        }
    }

    public static String getCommonProperty(String propertyKey) {
        LinkRedisClient jedis = getJedis();
        String result = jedis.hget("APPLICATION_PARM_ALL", propertyKey);
        returnResource(jedis);
        return result;
    }

    public static String getCropProperty(String propertyKey, String corpId) {
        LinkRedisClient jedis = getJedis();
        String key = "APPLICATION_PARM_" + corpId;
        String result = jedis.hget(key, propertyKey);
        returnResource(jedis);
        return result;
    }
}
